package com.tracy.lifeproject.activity;

import android.app.Activity;
import android.app.Service;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.tracy.lifeproject.R;
import com.tracy.lifeproject.bean.SearchBean;
import com.tracy.lifeproject.constant.UrlString;
import com.tracy.lifeproject.utils.OKHttpUtils;
import com.tracy.lifeproject.view.SearchTagFlowLayout;

import java.io.IOException;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * ***********************************************************
 * author: tracy
 * time: 2016/7/24 15:30
 * name:
 * overview:
 * usage: 主页的ToolBar搜索按钮点击后的跳转界面
 * *************************************************************
 */
public class SearchActivity extends Activity {
    @InjectView(R.id.tv_back)
    TextView mTvBack;
    @InjectView(R.id.iv_delete)
    ImageView mIvDelete;
    @InjectView(R.id.tv_search)
    TextView mTvSearch;
    @InjectView(R.id.fl_tag)
    SearchTagFlowLayout mFlTag;
    @InjectView(R.id.tb_search)
    Toolbar mTbSearch;
    private static final String TAG = "tag";
    @InjectView(R.id.et_keyword)
    TextView mEtKeyword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.inject(this);
        mTbSearch.getBackground().setAlpha(1);
        initData();
    }

    private void initData() {
        OKHttpUtils.newInstance(this).getAsyncMainData(UrlString.URL_HOTWORDS, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {
                Toast.makeText(SearchActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String result) {
                SearchBean searchBean = new Gson().fromJson(result, SearchBean.class);
                final List<String> hot_words = searchBean.getData().getHot_words();
                for (int i = 0; i < hot_words.size(); i++) {
                    TextView tvHotWord = new TextView(SearchActivity.this);
                    final String text = hot_words.get(i);
                    tvHotWord.setText(text);
                    ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    lp.leftMargin = 26;
                    lp.topMargin = 10;
                    lp.bottomMargin = 10;
                    tvHotWord.setPadding(20, 8, 20, 8);
                    tvHotWord.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                    tvHotWord.setBackgroundDrawable(getResources().getDrawable(R.drawable.search_hotword_shape));
                    mFlTag.addView(tvHotWord, lp);
                    tvHotWord.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mEtKeyword.setText(text);
                        }
                    });
                }
            }
        }, TAG);

    }

    @OnClick({R.id.tv_back, R.id.iv_delete, R.id.tv_search, R.id.et_keyword})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.iv_delete:
                mEtKeyword.setText("");
                break;
            case R.id.tv_search:
                if (mTvSearch.getText().equals("搜索")) {
                    mTvSearch.setBackground(getResources().getDrawable(R.mipmap.ic_menu_sort));
                    mTvSearch.setText("");
                } else {
                    mTvSearch.setBackground(null);
                    mTvSearch.setText("搜索");
                }
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        OKHttpUtils.newInstance(this).cancel(TAG);
    }
}
